﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.Serialization;
using System.IO;
using System.Xml.Serialization;

namespace Chapter05_Lab03
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person(1, "Pedro", "Brasil", true);

            FileStream fs = new FileStream("teste.txt", FileMode.Create);

            XmlSerializer xs = new XmlSerializer(typeof(Person));

            xs.Serialize(fs, p);

            fs.Close();
        }
    }

    [Serializable]
    public class Person :ISerializable
    {
        public int age;
        public string name;
        public string country;
        public bool married;


        public Person(int age, string name, string country, bool married)
        {
            this.age = age;
            this.name = name;
            this.country = country;
            this.married = married;
        }

        public Person()
        {
        }

        public override string ToString()
        {
            return String.Format("Age = {0}, Name = {1}, Country = {2}, Married = {3}", this.age, this.name, this.country, this.married);
        }

        #region ISerializable Members

        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Name", this.name);
            info.AddValue("The Country", this.country);
        }

        protected Person(SerializationInfo info, StreamingContext context)
        {
            this.name = info.GetString("Name");
            this.country = info.GetString("The Country");
        }

        #endregion
    }
}
